package com.dq.controller.activity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.ActivityRecordQuery;
import com.dq.controller.in.ActivityQuery;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import com.dq.domain.activity.Activity;
import com.dq.domain.activity.ActivityRecord;
import com.dq.domain.base.AbstractEntity;

import com.dq.domain.sys.section.SysSection;
import com.dq.domain.setup.GiftGrantSet;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author luchangtong
 * @Date 2019/11/22 17:23
 */
@RestController
@RequestMapping("/op/boss/activity")
@Api(description = "活动管理")
public class BossActivityController extends AllService {

    @ApiOperation("活动列表")
    @PostMapping("/list")
    @RequiresPermissions("sys:activity:list")
    public Vo list(@RequestBody ActivityQuery activityQuery) throws AdminException{
        return activityService.list(activityQuery);
    }
    @ApiOperation("添加活动")
    @PostMapping("/add")
    public Vo add(@RequestBody Activity activity) throws AdminException{
        //校验添加的新项目内  部分非空字段是否为空
        check(activity);
        //获取活动编号
        activity.setActivityNo(AbstractEntity.generyStrAndMstime(""));
        if(activity.getGiftGrantSetId() != null){
            GiftGrantSet giftGrantSet = dao.get(GiftGrantSet.class,activity.getGiftGrantSetId());
            activity.setGiftGrantSet(giftGrantSet);
        }
        SysSection sysSection = dao.get(SysSection.class,activity.getOrgId());
        activity.setOrg(sysSection);
        activityRepository.save(activity);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("修改活动")
    @PostMapping("/edit/{activityId}")
    public Vo edit(@PathVariable @ApiParam("活动id") Long activityId,@RequestBody Activity editActivity) throws AdminException{
        check(editActivity);
        Activity activity = dao.get(Activity.class,activityId);
        if(activity == null){
            throw new AdminException("该活动不存在");
        }
        if(editActivity.getGiftGrantSet()!=null && activity.getGiftGrantSet().getId() != editActivity.getGiftGrantSetId()){
            GiftGrantSet giftGrantSet = dao.get(GiftGrantSet.class,activity.getGiftGrantSetId());
            activity.setGiftGrantSet(giftGrantSet);
        }
        SysSection sysSection = dao.get(SysSection.class,editActivity.getOrgId());
        activity.setOrg(sysSection);
        BeanUtils.copyProperties(editActivity,activity, BeanNullUtil.getNullPropertyNames(editActivity));
        activityRepository.save(activity);
        return new Vo(HttpStatus.OK);
    }

      @ApiOperation("删除活动")
    @PostMapping("/delete/{activityId}")
    public Vo delete(@PathVariable @ApiParam("活动id") Long activityId) throws AdminException{
        Activity activity = dao.get(Activity.class,activityId);
        if(activity == null){
            throw new AdminException("活动不存在");
        }
        activity.setDeleted(true);
        activityRepository.save(activity);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("活动参与记录")
    @PostMapping("activityRecordList")
    public Vo list(@RequestBody ActivityRecordQuery activityRecordQuery) throws AdminException{
        if(null==activityRecordQuery.getPage() || null==activityRecordQuery.getSize()){
            throw new AdminException("缺少必要的请求参数");
        }
        List<ActivityRecord> records = activityRecordService.findByWhere(activityRecordQuery);
        Long total = activityRecordService.countByWhere(activityRecordQuery);
        List<ActivityRecord.UserActivityRecordVo> list = new ArrayList<>();
        records.forEach(record ->{
            list.add(record.recordList());
        });
        PageInfo pageInfo = new PageInfo(list);
        pageInfo.setTotal(total);
        return new Vo(pageInfo);
    }

    @ApiOperation("所有的活动")
    @GetMapping("findAllActivityList")
    public Vo findAllActivityList(){
        return new Vo(activityRepository.findByDeletedFalse());
    }

    @GetMapping("/getAllGrant")
    @ApiOperation("获取 所有门店设置的礼物列表")
    public Vo getAllGrant()throws AdminException{
//        SysSection section = sysUserService.getUser().getSection();
        List<GiftGrantSet> giftGrantSets = giftGrantSetRepository.findByDeletedFalse();
        JSONArray array = new JSONArray();
        giftGrantSets.forEach(giftGrantSet -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id",giftGrantSet.getId());
            jsonObject.put("name",giftGrantSet.getCategoryName());
            array.add(jsonObject);
        });
        return new ListDataVo(array);
    }


    /**
     * 私有的方法   校验添加新项目时 部分费用不能为空
     * @param activity
     * @throws AdminException
     */
    public void check(Activity activity) throws AdminException{
        if(Activity.ActivityCostType.charge.toString().equals(activity.getActivityCostType().toString())){
            if(activity.getNormalPrice() == null){
                throw new AdminException("正常费用不能为空");
            }
            if(activity.getMemberPrice() == null){
                throw new AdminException("会员费用不能为空");
            }
        }
        if(Activity.ActivityType.subcard.toString().equals(activity.getActivityType().toString())){
            if("".equals(activity.getServiceProject())){
                throw new AdminException("服务项目不能为空");
            }
            if(activity.getGiftGrantSetId() == null){
                throw new AdminException("礼物不能为空");
            }
        }
    }
}
